var fileinput = function(fileExtensions, filecount) {
	$("#luoshurong").fileinput({
        uploadAsync: false,
		// uploadUrl: "http://10.129.12.18:8080/beacon-upload/smcwebs",
		uploadUrl:"http://127.0.0.1/ml/file_upload/",
        uploadExtraData: function() {
            return {
            	title:$('#picture_title').val(),
                type: $("#fodder_style").val(),
                label: $("#span_array").val(),
            };
        },
		showUpload: false,	//	是否显示文件上传按钮
		showPreview:true,	//	是否显示文件预览
		allowedPreviewTypes:['image'],	//	可以显示预览的文件类型['image', 'html', 'text', 'video', 'audio', 'flash', 'object']
		maxfileSize:0,	//	上传文件大小限制，单位为kb，0为不限制大小
//		filebatchselected:true,	//	是否隐藏预览内容
//		purifyhtml:false,	//	是否净化内容预览HTML内容类型显示
//		uploadAsync:false,	//	是否异步上传
//		showCaption: false,	//	是否显示文件标题
		browseClass: "btn btn-primary btn-lg",
		fileType: "any",
		previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
		overwriteInitial: false,
		initialPreviewAsData: true,
//		allowedFileTypes:['image'],
		allowedFileExtensions: fileExtensions, //['jpg','png','gif']
		maxFileCount: filecount, //表示允许同时上传的最大文件个数
		//      initialPreview: [
		//          "http://lorempixel.com/1920/1080/transport/1",
		//          "http://lorempixel.com/1920/1080/transport/2",
		//          "http://lorempixel.com/1920/1080/transport/3"
		//      ],
		//      initialPreviewConfig: [
		//          {caption: "transport-1.jpg", size: 329892, width: "120px", url: "{$url}", key: 1},
		//          {caption: "transport-2.jpg", size: 872378, width: "120px", url: "{$url}", key: 2},
		//          {caption: "transport-3.jpg", size: 632762, width: "120px", url: "{$url}", key: 3}
		//      ]





	});
}
$('#luoshurong').on('filebatchuploadsuccess', function (event, data, previewId, index) {
    var form = data.form, files = data.files, extra = data.extra,
        response = data.response, reader = data.reader;

    $("#luoshurong").fileinput("reset")
});
 $("#luoshurong").on("fileuploaded", function(event, data, previewId, index) {
    alertView("上传成功!");
});
fileinput(['gif', 'jpg', 'jpeg', 'png', 'gif', 'gif', 'jpg', 'png', 'bmp'], 10) //gif,jpg,jpeg,png,gif,jpg,png,bmp
$("#uploader_fileinput").on("click", function () {
    if ($("#picture_title").val() == '') {
    	alert("素材標題不能爲空")
    } else if($("#fodder_style").val() == ''){
    	alert("請選擇素材類型")
    } else if($("#span_array").val() == ''){
    	alert("請添加標籤")
    } else{
    	if ($("#luoshurong").fileinput('getFilesCount') == 0) {
	        alert("请您选择上传文件")
	    } else if ($("#luoshurong").fileinput('getFilesCount') == 1) {
	        if ($("#fodder_style").find("option:selected").attr("value1") == "psd") {
//	            alert("请您上传两张图片一张psd后缀一张png、jpg、png、bmp等后缀图片")
				alert("上傳需求：一個PSD素材 + 一張圖片")
	        } else if ($("#fodder_style").find("option:selected").attr("value1") == "3d") {
//	            alert("请您上传两张图片一张3ds、max、mtl、obj、dwg、lwo、vrml后缀一张png、jpg、png、bmp等后缀图片")
	            alert("上傳需求：一個3D素材 + 一張圖片")
	        } else if ($("#fodder_style").find("option:selected").attr("value1") == "image") {
	            var files = $('#luoshurong').fileinput('getFileStack'); // returns file list selected
	            var f = files[0]
	            var reader = new FileReader()
	            var image = new Image()
	            var width = image.width
	            var height = image.height
	            reader.onload = function (e) {
	                var data = e.target.result
	                var image = new Image()
	                image.onload = function () {
	                     width = image.width
	                     height = image.height
	                }
	                image.src = data
	            }
	            reader.readAsDataURL(f);
	            $("#luoshurong").fileinput("upload"); //上传
	        }
	    } else if ($("#luoshurong").fileinput('getFilesCount') == 2 &&(($("#fodder_style").find("option:selected").attr("value1") == "psd") || ($("#fodder_style").find("option:selected").attr("value1") == "3d")) ) {
	        // alert($("#luoshurong").fileinput("getFilesCount"))
	        // var myfile = document.getElementById('luoshurong');
	        var files = $('#luoshurong').fileinput('getFileStack'); // returns file list selected
	        var file1 = files[0].name//获取上传图片的名字
	        var filesize0 = files[0].size
	        var filesize1 = files[1].size//看后台要求filesize1/1024/1024 得到的事MB单位 filesize1/1024得到是KB单位
	        // var upFileName = $("#fileToUpload").val();
	        var index1 = file1.lastIndexOf(".");
	        var index2 = file1.length;
	        var suffix = file1.substring(index1 + 1, index2);//后缀名
	        var file2 = files[1].name
	        var index3 = file2.lastIndexOf(".")
	        var index4 = file2.length
	        var suffix1 = file2.substring(index3 + 1, index4)//后缀名
	        if (suffix.toUpperCase() == "JPG"
	            || suffix.toUpperCase() == "PNG"
	            || suffix.toUpperCase() == "GIF"
	            || suffix.toUpperCase() == "BMP") {
	            // alert("第一个是图片"+suffix1)
	            var f = files[0]
	            var reader = new FileReader()
	            var image = new Image()
	            var width = image.width
	            var height = image.height
	            reader.onload = function (e) {
	                var data = e.target.result
	                var image = new Image()
	                image.onload = function () {
	                     width = image.width
	                     height = image.height
	                    // alert("suffix1" + width + "======" + height + "=======" + files[1].size)
                        // alert('success');
	                }
	                image.src = data
	            }
	            reader.readAsDataURL(f);
	        }
	        if (suffix1.toUpperCase() == "JPG"
	            || suffix1.toUpperCase() == "PNG"
	            || suffix1.toUpperCase() == "GIF"
	            || suffix1.toUpperCase() == "BMP") {
	            // alert("第二个是图片"+suffix1)
	            var f = files[1]
	            var reader = new FileReader()
	            reader.onload = function (e) {
	                var data = e.target.result
	                var image = new Image()
	                image.onload = function () {
	                     width = image.width
	                     height = image.height
	                    // alert("suffix1" + width + "======" + height + "=====" + files[0].size)
	                }
	                image.src = data
	            }
	            reader.readAsDataURL(f);
	        }
	        if ((suffix1 == 'psd' || suffix == 'psd')) {
	            if (suffix == 'psd') {
	                if (suffix1.toUpperCase() == "BMP" ||
	                    suffix1.toUpperCase() == "JPG" ||
	                    suffix1.toUpperCase() == "JPEG" ||
	                    suffix1.toUpperCase() == "PNG" ||
	                    suffix1.toUpperCase() == "GIF") {
	                    $("#luoshurong").fileinput("upload"); //上传
	                } else {
	                    alert("请您上传一张BMP、JPG、JPEG、PNG、GIF等后缀图片")
	                }
	            } else if (suffix1 == 'psd') {
	                if (suffix.toUpperCase() == "BMP" ||
	                    suffix.toUpperCase() == "JPG" ||
	                    suffix.toUpperCase() == "JPEG" ||
	                    suffix.toUpperCase() == "PNG" ||
	                    suffix.toUpperCase() == "GIF") {
	                    $("#luoshurong").fileinput("upload"); //上传
	                } else {
	                    alert("请您上传一张BMP、JPG、JPEG、PNG、GIF后缀图片")
	                }
	            }
	        }
	        else if ((suffix1.toUpperCase() == '3DS' || suffix.toUpperCase() == '3DS') ||
	            (suffix1.toUpperCase() == 'MAX' || suffix.toUpperCase() == 'MAX') ||
	            (suffix1.toUpperCase() == 'MTL' || suffix.toUpperCase() == 'MTL') ||
	            (suffix1.toUpperCase() == 'OBJ' || suffix.toUpperCase() == 'OBJ') ||
	            (suffix1.toUpperCase() == 'DWG' || suffix.toUpperCase() == 'DWG') ||
	            (suffix1.toUpperCase() == 'LWO' || suffix.toUpperCase() == 'LWO') ||
	            (suffix1.toUpperCase() == 'VRML' || suffix.toUpperCase() == 'VRML')) { //3ds,max,mtl为3d特效的后缀名z
	//          alert("3ds")
	            if (suffix1.toUpperCase() == '3DS' || suffix1.toUpperCase() == 'MAX' || suffix1.toUpperCase() == 'MTL'
	                || suffix1.toUpperCase() == 'OBJ' || suffix1.toUpperCase() == 'DWG' || suffix1.toUpperCase() == 'LWO' || suffix1.toUpperCase() == 'VRML') {
	                if (suffix.toUpperCase() == "BMP" ||
	                    suffix.toUpperCase() == "JPG" ||
	                    suffix.toUpperCase() == "JPEG" ||
	                    suffix.toUpperCase() == "PNG" ||
	                    suffix.toUpperCase() == "GIF") {
	                    $("#luoshurong").fileinput("upload"); //上传
	                } else {
	                    alert("请您上传一张BMP、JPG、JPEG、PNG、GIF等后缀图片")
	                }
	            } else if (suffix == '3ds' || suffix == 'max' || suffix == 'mtl') {
	                if (suffix1.toUpperCase() == "BMP" ||
	                    suffix1.toUpperCase() == "JPG" ||
	                    suffix1.toUpperCase() == "JPEG" ||
	                    suffix1.toUpperCase() == "PNG" ||
	                    suffix1.toUpperCase() == "GIF") {
	                    $("#luoshurong").fileinput("upload"); //上传
	                } else {
	                    alert("请您上传一张BMP、JPG、JPEG、PNG、GIF等后缀图片")
	                }
	            }
	        } else {
	            if ($("#fodder_style").find("option:selected").attr("value1") == "psd") {
	                if ((suffix1.toUpperCase() != 'PSD' && suffix.toUpperCase() != "PSD" )) {//|
	                    alert("请您选择一张psd为后缀的文件")//第一次效| (suffix.toUpperCase() != 'PSD' && suffix1.toUpperCase()!="PSD")果
	                } else {
	                    $("#luoshurong").fileinput("upload"); //上传
	                }
	            } else if ($("#fodder_style").find("option:selected").attr("value1") == "3d") {
	                if ((suffix1.toUpperCase() != '3DS' && suffix.toUpperCase() != '3DS') ||
	                    (suffix1.toUpperCase() != 'MAX' && suffix.toUpperCase() != 'MAX') ||
	                    (suffix1.toUpperCase() != 'MTL' && suffix.toUpperCase() != 'MTL') ||
	                    (suffix1.toUpperCase() != 'OBJ' && suffix.toUpperCase() != 'OBJ') ||
	                    (suffix1.toUpperCase() != 'DWG' && suffix.toUpperCase() != 'DWG') ||
	                    (suffix1.toUpperCase() != 'LWO' && suffix.toUpperCase() != 'LWO') ||
	                    (suffix1.toUpperCase() != 'VRML' && suffix.toUpperCase() != 'VRML')) {
	                    alert("请选择一张3ds、max、mtl、obj、dwg、lwo、vrml为后缀的文件")
	                } else {
	                    $("#luoshurong").fileinput("upload"); //上传
	                }
	            }
	        }
	    }
	    else {
			var files = $('#luoshurong').fileinput('getFileStack'); // returns file list selected
			for (i=0;i<$("#luoshurong").fileinput("getFilesCount");i++){
				var f = files[i]
				var reader = new FileReader()
				var image = new Image()
				//var width = image[i].width
				//var height = image[i].height
				reader.onload = function (e) {
					var data = e.target.result
					var image = new Image()
					image.onload = function () {
						//width = image[i].width
						//height = image[i].height

						//img_width.push(width);
						//img_hegit.push(height);
						//img_size.push(((files[i].size)/1024/1024).toFixed(2))
						//alert("老实人1")
						//alert("尺寸："+img_width+'*'+img_hegit +'px;  体积：'+img_size+'M')
					}
					image.src = data
				}
				reader.readAsDataURL(f);
			}

			$("#luoshurong").fileinput("upload"); //上传
		}
    }
    //----------------------add ajax
})
$("#fodder_style").on("change", function () {
	$(this).css("color", "#000000")
    $("#uploader").empty()
    $("#uploader").html('<input  name="beacon" id="luoshurong" type="file" multiple="true" data-show-upload="false" data-preview-file-type="any" />')
    var Suffix_value = $(this).val()
    var Suffix_value1 = $(this).find("option:selected").attr("value1")
    var fileExtensions = Suffix_value.split(",")
    if (Suffix_value1 == "psd" || Suffix_value1 == '3d') {
        fileinput(fileExtensions, 2)
    } else {
        fileinput(['gif', 'jpg', 'jpeg', 'png', 'gif', 'gif', 'jpg', 'png', 'bmp'], 10)
    }
})
var span_array = [];
$("#add_span").click(function() {
	span_array = [];
	var span_val = $("#span_value").val();
	if(span_val==""){
		alert("Can not be empty");
	}else{
		$(".span_show").append('<li class="span_self" onclick="span_detach(this)" value="'+span_val+'"><a>' + span_val + '</a></li>');
		$("#span_value").val('');
		$(".span_self").each(function(){
			span_array.push($(this).attr('value'));
		})
		$("#span_array").val(span_array);
	}
});
console.log(span_array);
function span_detach(obj) {
	$(obj).detach();
}